function clear_comment_replies()  {
    $(".replying_to").each(function () {
        // Retrieve old data
        $(this).empty();
        $(this).append($(this).data("link"));

        // Change class
        $(this).removeClass("replying_to").addClass("reply_to");
    });
}

function reply_to(action, comment_id, username, cancel) {
    // Change form action
    $("#post_comment_form").attr('action', action);

    // Create post text
    var post_text = "You are replying to " + username +  "'s <a href=#" + comment_id + ">comment:</a> ";
    post_text += "<span class='cancel_reply'><a href=\"javascript:cancel_reply('" + cancel + "')\">Cancel reply</a></span>";

    // Change post text
    var post_comment = document.getElementById("post_comment");
    post_comment.innerHTML = post_text;

    // Remove all replies
    clear_comment_replies();

    // Remove link from replied comment
    var comment = $("#reply_to_" + comment_id);
    comment.data("link", comment.find("a"));
    comment.text("Replying to");

    // Change class
    comment.removeClass("reply_to").addClass("replying_to");

    // Set focus
    var comment_textarea = document.getElementById('id_comment');
    comment_textarea.focus();
}

function cancel_reply(action) {
    // Change form action
    $("#post_comment_form").attr('action', action);

    // Change post text
    var post_comment = document.getElementById("post_comment");
    post_comment.innerHTML = "Post a comment:";

    // Remove all replies
    clear_comment_replies();
}
